﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Objects;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;

namespace SwitData
{
    public class SwitchRepository
    {
        public IList<Switch> GetSwitchList()
        {
            SwitDataEntities switchEntities = new SwitDataEntities();
            var list =  switchEntities.Switches.ToList();
            return list;
        }

        public Switch GetSwitch(int id)
        {
            SwitDataEntities switchEntities = new SwitDataEntities();
            var eSwitch = switchEntities.Switches.Where(s=> s.ID == id).FirstOrDefault();
            return eSwitch;
        }

        public void AddSwitch(Switch eSwitch)
        {
            SwitDataEntities switchEntities = new SwitDataEntities();                        
            switchEntities.Switches.Attach(eSwitch);
            switchEntities.Switches.Add(eSwitch);
            switchEntities.SaveChanges();            
        }

        public void DeleteSwitch(int SwitchID) 
        {
            SwitDataEntities switchEntities = new SwitDataEntities();
            var eSwtich = switchEntities.Switches.Where(s => s.ID == SwitchID).FirstOrDefault();
            switchEntities.Switches.Remove(eSwtich);
            switchEntities.SaveChanges();            
        }

        public IList<SwitchType> GetSwitchTypeList()
        {
            SwitDataEntities switchEntities = new SwitDataEntities();
            var list = switchEntities.SwitchTypes.ToList();
            return list;
        }
    }

    [DataContract]
    public class PortableSwitch
    {
        [Required]
        [Display(Name = "Switch ID")]
        [DataMember]
        public int ID { get; set; }

        [Required]
        [Display(Name = "Switch Adı")]
        [DataMember]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Switch IP")]
        [DataMember]
        public string IP { get; set; }

        [Required]
        [Display(Name = "Switch PORT")]
        [DataMember]
        public int PORT { get; set; }

        [Required]
        [Display(Name = "Switch Type ID")]
        [DataMember]
        public int SwitchTypeID { get; set; }

        [Required]
        [Display(Name = "Kullanıcı Adı")]
        [DataMember]
        public string UserName { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Şifre")]
        [DataMember]
        public string Password { get; set; }

        public static List<PortableSwitch> TranslateSwitchEntityListToPortableSwitchList(IList<SwitData.Switch> eSwitchList)
        {
            List<PortableSwitch> SwitchList = new List<PortableSwitch>();
            foreach (var eSwitch in eSwitchList)
            {
                SwitchList.Add(TranslateSwitchEntityToPortableSwitch(eSwitch));
            }
            return SwitchList;
        }
        public static PortableSwitch TranslateSwitchEntityToPortableSwitch(Switch eSwitch)
        {
            PortableSwitch pSwitch = new PortableSwitch();
            pSwitch.ID = eSwitch.ID;
            pSwitch.IP = eSwitch.IP;
            pSwitch.Name = eSwitch.Name;
            pSwitch.Password = eSwitch.Password;
            pSwitch.PORT = eSwitch.Port;
            pSwitch.SwitchTypeID = eSwitch.SwitchTypeID;
            pSwitch.UserName = eSwitch.UserName;
            return pSwitch;
        }

        public static List<Switch> TranslatePortableSwitchListtoSwitchEntityList(IList<SwitData.PortableSwitch> pSwitchList)
        {
            List<Switch> SwitchList = new List<Switch>();
            foreach (var pSwitch in pSwitchList)
            {
                SwitchList.Add(TranslatePortableSwitchtoSwitchEntity(pSwitch));
            }
            return SwitchList;
        }

        public static Switch TranslatePortableSwitchtoSwitchEntity(PortableSwitch pSwitch)
        {
            Switch eSwitch = new Switch();
            eSwitch.ID = pSwitch.ID;
            eSwitch.IP = pSwitch.IP;
            eSwitch.Name = pSwitch.Name;
            eSwitch.Password = pSwitch.Password;
            eSwitch.Port = pSwitch.PORT;
            eSwitch.SwitchTypeID = pSwitch.SwitchTypeID;
            eSwitch.UserName = pSwitch.UserName;
            return eSwitch;
        }
    }

    [DataContract]
    public class PortableSwitchType
    {
        [Required]
        [Display(Name = "Switch Tip ID")]
        [DataMember]
        public int ID { get; set; }

        [Required]
        [Display(Name = "Switch Tip Adı")]
        [DataMember]
        public string Name { get; set; }

        public static List<PortableSwitchType> TranslateSwitchEntityListToSwitchTypeList(IList<SwitData.SwitchType> eSwitchTypeList)
        {
            List<PortableSwitchType> SwitchTypeList = new List<PortableSwitchType>();
            foreach (var eSwitchType in eSwitchTypeList)
            {
                SwitchTypeList.Add(TranslateSwitchTypeEntityToSwitchType(eSwitchType));
            }
            return SwitchTypeList;
        }
        public static PortableSwitchType TranslateSwitchTypeEntityToSwitchType(SwitData.SwitchType eSwitchType)
        {
            PortableSwitchType dcSwitchType = new PortableSwitchType();
            dcSwitchType.ID = eSwitchType.ID;
            dcSwitchType.Name = eSwitchType.Name;
            return dcSwitchType;
        }
    }
}
